package com.lianniu.mgr.saleManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lianniu.mgr.saleManage.model.SkuProductLogConditionVo;
import com.lianniu.mgr.saleManage.model.SkuProductLogVo;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.buyerManage.dao.IBuyerManageDao;
import com.lianniu.mgr.buyerManage.vo.BuyerVo;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductModelVo;
import com.lianniu.mgr.saleManage.dao.ISkuProductDao;
import com.lianniu.mgr.saleManage.model.SkuProductConditionVo;
import com.lianniu.mgr.saleManage.model.SkuProductVo;
import com.lianniu.mgr.saleManage.service.ISkuProductService;
import com.lianniu.mgr.supplyManage.dao.ISupplyManageDao;
import com.lianniu.mgr.supplyManage.vo.SupplyVo;
import com.lianniu.mgr.util.StringUtil;
@Service
public class SkuProductServiceImpl implements ISkuProductService {
	
	private static final Logger logger = Logger.getLogger(SkuProductServiceImpl.class);
	
	@Autowired
	ISkuProductDao skuProductDao;
	@Autowired
	ISupplyManageDao supplyManageDao;
	@Autowired
	IBuyerManageDao buyerManageDao;

	@Override
	public BaseResult insertSkuProduct(SkuProductVo skuProduct,String mobile,String countyId) {
		logger.info("insertSkuProduct begin");
		logger.info("insertSkuProduct check data begin"+skuProduct);
		String saleStatus = skuProduct.getSaleStatus();
		List<SupplyVo> supplyVoList = null;
		List<BuyerVo> buyerVoList = null;
		String buyerId = "";
		int i = 0;
		//链牛指向卖家
		if(saleStatus.equals("1")){
		    supplyVoList = supplyManageDao.getSupplyIdByMobile(mobile);
	        if(supplyVoList != null){
	            buyerVoList = buyerManageDao.getBuyerIdByMobile(mobile);
	            for(BuyerVo b:buyerVoList){
	                buyerId += b.getBuyerId()+",";
	            }
	            skuProduct.setBuyerId(buyerId);
	            i = skuProductDao.insertSkuProduct(skuProduct);
	        } 
		}else if(saleStatus.equals("0")){//链牛指向买家
		    buyerVoList = buyerManageDao.getBuyerIdByMobile(mobile);
		    for(BuyerVo b:buyerVoList){
                buyerId += b.getBuyerId()+",";
            }
		    skuProduct.setBuyerId(buyerId);
		    skuProduct.setSaleType("2");
		    i = skuProductDao.insertSkuProduct(skuProduct);
		}else {//链牛指向区域
		    skuProduct.setAreaId(countyId);
		    skuProduct.setSaleType("1");
		    i = skuProductDao.insertSkuProduct(skuProduct);
		}
		logger.info("insertSkuProduct skuProduct"+JSON.toJSONString(skuProduct));
		return BaseResult.success(i);
	}

	@Override
	public BaseResult selectBySkuProductForPage(SkuProductConditionVo condition,PaginationInfo paginationInfo) {
		logger.info("selectBySkuProductForPage begin");
	    
		condition.setPaginationInfo(paginationInfo);
	    List<SkuProductVo> list = skuProductDao.selectBySkuProductForPage(condition);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectBySkuProductForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult updateById(String...id) {
		logger.info("updateById begin");
	    logger.info("updateById ids:" +id.toString());
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(skuProductDao.updateSkuProductBatch(id));
	}

	@Override
	public BaseResult deleteSkuProductBatch(String... id) {
		logger.info("deleteSkuProductBatch begin");
	    logger.info("deleteSkuProductBatch Ids:" +id.toString());
	    logger.info("deleteSkuProductBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteSkuProductBatch: input data check end");
	    
	    return BaseResult.success(skuProductDao.deleteSkuProductBatch(id));
	}

	@Override
	public BaseResult selectById(SkuProductVo skuProduct) {
		logger.info("selectById begin");
	    logger.info("selectById Id:" +skuProduct.getId());
	  
	    return BaseResult.success(skuProductDao.selectById(skuProduct.getId()));
	}

    @Override
    public BaseResult updateStatus(String id) {
        logger.info("updateStatus begin");
        return BaseResult.success(skuProductDao.updateSkuProduct(id));
    }

    @Override
    public BaseResult getAreaName(String areaId) {
       List<SkuProductVo> list = skuProductDao.getAreaName(areaId);
        return BaseResult.success(list);
    }

    @Override
    public BaseResult getSkuProductById(String id) {
        SkuProductVo skuProductVo = skuProductDao.getSkuProductById(id);
        return BaseResult.success(skuProductVo);
    }

    @Override
    public BaseResult checkApproveStatus(String approveStatus, String id) {
        if(StringUtils.isBlank(id)){
            return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
          }
            boolean result = skuProductDao.checkApproveStatus(approveStatus, id);
            logger.info(id+"审核结果为："+result);
            return BaseResult.success();
    }

	@Override
	public BaseResult modifySkuProduct(SkuProductVo skuProductVo) {
		logger.info("modifySkuProduct begin");
		logger.info("modifySkuProduct id:" +skuProductVo.getId().toString());
		logger.info("modifySkuProduct: input data check begin");

		BaseResult baseResult;
		if (StringUtil.isEmpty(skuProductVo.getId().toString())) {
			baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
			return baseResult;
		}
		logger.info("modifySkuProduct: input data check end");
		return BaseResult.success(skuProductDao.modifySkuProduct(skuProductVo));
	}

	@Override
	public BaseResult selectBySkuProductLogForPage(SkuProductLogConditionVo conditionV, PaginationInfo paginationInfo) {
		logger.info("selectBySkuProductLogForPage begin");

		conditionV.setPaginationInfo(paginationInfo);
		List<SkuProductLogVo> list = skuProductDao.selectBySkuProductLogForPage(conditionV);
		Map<String,Object> retmap=new HashMap<String,Object>();
		retmap.put("total", paginationInfo.getTotalRecord());
		retmap.put("curPage", paginationInfo.getCurPage());
		retmap.put("rows", list);
		logger.info("selectBySkuProductLogForPage:  end");

		return BaseResult.success(retmap);
	}

}
